<?php

class mscitacisystem{
	
	public function vypis(){
		conndb::getInst()->query("SELECT b.*, c.Meno AS kat, a.meno, a.id AS kolo FROM sutazne_kolo a 
								INNER JOIN scitaci_system b on (a.id_scitaci_system=b.id) 
								INNER JOIN sutazna_kategoria c on (c.id=a.id_sutazna_kategoria )
								WHERE a.id_sutaz=\"".$_SESSION['idupravsu']."\" ORDER BY c.Meno ");
		
		return conndb::getInst()->getRowsAsArrayOfObjects();	
	}
	
	public function kontrola($meno){
		conndb::getInst()->query("SELECT * FROM scitaci_system WHERE meno='".$meno."'");
		$possible = conndb::getInst()->getRowAsObject();	
		if ($possible='' or empty($possible)){
			return true;
		} else {
			return false;
		}
	}
	public function kola(){	
		$majukola='';
		$bezkat='';
		conndb::getInst()->query("SELECT id_sutazna_kategoria FROM sutazne_kolo WHERE meno IS NOT NULL AND id_scitaci_system IS NOT NULL");
		$kat =conndb::getInst()->getRowsAsArrayOfObjects();	
		if (!empty($kat)){
			foreach($kat as $k){
				$bezkat = $bezkat." a.id_sutazna_kategoria !='".$k->id_sutazna_kategoria ."' AND ";
			}
		}
		
		conndb::getInst()->query("SELECT a.id, a.meno, b.Meno FROM sutazne_kolo a INNER JOIN sutazna_kategoria b on (b.id=a.id_sutazna_kategoria) WHERE a.id_scitaci_system IS NULL AND a.meno IS NOT NULL AND a.id_sutaz='".$_SESSION['idupravsu']."' ORDER BY b.Meno");
		$katskolami =conndb::getInst()->getRowsAsArrayOfObjects();	
		//var_dump($katskolami);die;
		if (empty($katskolami)){
			conndb::getInst()->query("SELECT a.id, a.meno, b.Meno FROM sutazne_kolo a 
			INNER JOIN sutazna_kategoria b on (b.id=a.id_sutazna_kategoria)
			WHERE ".$bezkat." a.id_scitaci_system IS NULL AND a.meno IS NULL AND a.id_sutaz='".$_SESSION['idupravsu']."' 
			ORDER BY b.Meno");
			$vsetky = conndb::getInst()->getRowsAsArrayOfObjects();
		} else {
			foreach($katskolami as $kola){
				$majukola = $majukola." b.Meno!='".$kola->Meno."' AND ";
			}
			conndb::getInst()->query("SELECT a.id, a.meno, b.Meno FROM sutazne_kolo a INNER JOIN sutazna_kategoria b on (b.id=a.id_sutazna_kategoria) WHERE ".$majukola."a.id_scitaci_system IS NULL AND a.meno IS NULL AND a.id_sutaz='".$_SESSION['idupravsu']."' ORDER BY b.Meno");
			$ostatne = conndb::getInst()->getRowsAsArrayOfObjects();
			if (!empty($ostatne)){
				foreach($ostatne as $ost){
					array_push($katskolami, $ost);
				}
			}
			$vsetky = $katskolami;
			
		}
		return $vsetky;
	}
	public function pridat($idkolo, $menoss){
		conndb::getInst()->query("SELECT id FROM scitaci_system WHERE Meno='".$menoss."'");
		$idss = conndb::getInst()->getRowAsObject();
		conndb::getInst()->query("UPDATE sutazne_kolo SET id_scitaci_system='".$idss->id."' WHERE id='".$idkolo."'");
	}
	
	public function kotrolaupdate($id){
		conndb::getInst()->query("SELECT c.body FROM sutazne_kolo a
			INNER JOIN sutaziaci_v_kole b on (a.id=b.id_sutazne_kolo)
			INNER JOIN hodnotenie c on (b.id=c.id_sutaziaci_v_kole)
			WHERE a.id_scitaci_system='".$id."'");
		if ($possible='' or empty($possible)){
			return true;
		} else {
			return false;
		}
	}
	
	public function vytvor($menoss,$popisss, $bodyod, $bodydo, $zoradenie){
		if ($zoradenie=='vzostupne'){$zor=1;}
		if ($zoradenie=='zostupne'){$zor=2;}
		conndb::getInst()->query("INSERT INTO scitaci_system (Meno, Popis, Body_od, Body_do, Zoradenie) VALUES ('".mysql_real_escape_string($menoss)."',
				'".mysql_real_escape_string($popisss)."', '".mysql_real_escape_string($bodyod)."', '".mysql_real_escape_string($bodydo)."', '".$zor."') ");
	}
	
	public function nacitaj(){
		conndb::getInst()->query("SELECT * FROM scitaci_system WHERE id='".$_SESSION["idupravscitacisystem"]."'");
		return conndb::getInst()->getRowAsObject();	
	}
	
	public function update($menoss,$popisss, $bodyod, $bodydo, $zoradenie){
		if ($zoradenie=='vzostupne'){$zor=1;} 
		if ($zoradenie=='zostupne'){$zor=2;}
		conndb::getInst()->query("UPDATE scitaci_system SET Meno='".mysql_real_escape_string($menoss)."', Popis='".mysql_real_escape_string($popisss)."', Body_od='".mysql_real_escape_string($bodyod)."',
			Body_do='".mysql_real_escape_string($bodydo)."', Zoradenie='".$zor."' WHERE id='".$_SESSION["idupravscitacisystem"]."'");
	
	}	
	public function delete($id){
		conndb::getInst()->query("UPDATE sutazne_kolo SET id_scitaci_system=NULL WHERE id='".$id."'");
		
	}

	
}
?>